<?php
/**
 * Created by PhpStorm.
 * User: MSQ
 * Date: 2021/9/27
 * Time: 16:54
 */

namespace App\Handler\WebSocket;


use App\Utils\ErrorMessage;
use App\Utils\LogHelp;
use App\Utils\WebSocketMessage;

class ExceptionHandler
{
    public static function handle(\Swoole\WebSocket\Server $server, \Throwable $throwable, string $raw, \EasySwoole\Socket\Client\WebSocket $client, \EasySwoole\Socket\Bean\Response $response)
    {
        // log
        LogHelp::error("原始数据：{$raw}\n错误描述：{$throwable->__toString()}", 'websocket');

        try {
            $data = json_decode($raw, true);
            $id = $data['Id'] ?? 0;

            if ($id) {
//                LogHelp::console($id);
                $msg = WebSocketMessage::getErrorMessage($throwable->getMessage());
                $response->setMessage($msg);
            }
        } catch (\Exception $e) {
            // 解码失败
//            LogHelp::error("解码失败【原数据:{$raw}】", 'websocket');
//            return null;
        }

//        $response->setMessage('system error!');
//        $response->setStatus($response::STATUS_RESPONSE_AND_CLOSE);
    }
}
